package com.ruoyi.gis.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gis.domain.GisLayerProductGroup;
import com.ruoyi.gis.service.IGisLayerProductGroupService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * layer分组Controller
 *
 * @author rww
 * @date 2023-11-16
 */
@RestController
@RequestMapping("/gis/layerProductGroup")
public class GisLayerProductGroupController extends BaseController
{
    @Autowired
    private IGisLayerProductGroupService gisLayerProductGroupService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询layer分组列表
     */
//    @PreAuthorize("@ss.hasPermi('gis:layerProductGroup:list')")
    @GetMapping("/list")
    public TableDataInfo list(GisLayerProductGroup gisLayerProductGroup)
    {
        startPage();
        List<GisLayerProductGroup> list = gisLayerProductGroupService.selectGisLayerProductGroupList(gisLayerProductGroup);
        return getDataTable(list);
    }

    /**
     * 导出layer分组列表
     */
//    @PreAuthorize("@ss.hasPermi('gis:layerProductGroup:export')")
    @Log(title = "layer分组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GisLayerProductGroup gisLayerProductGroup)
    {
        List<GisLayerProductGroup> list = gisLayerProductGroupService.selectGisLayerProductGroupList(gisLayerProductGroup);
        ExcelUtil<GisLayerProductGroup> util = new ExcelUtil<GisLayerProductGroup>(GisLayerProductGroup.class);
        util.exportExcel(response, list, "layer分组数据");
    }

    /**
     * 获取layer分组详细信息
     */
//    @PreAuthorize("@ss.hasPermi('gis:layerProductGroup:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(gisLayerProductGroupService.selectGisLayerProductGroupById(id));
    }

    /**
     * 新增layer分组
     */
//    @PreAuthorize("@ss.hasPermi('gis:layerProductGroup:add')")
    @Log(title = "layer分组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody GisLayerProductGroup gisLayerProductGroup)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        gisLayerProductGroup.setUserId(loginUser.getUserId());
        return toAjax(gisLayerProductGroupService.insertGisLayerProductGroup(gisLayerProductGroup));
    }

    /**
     * 修改layer分组
     */
//    @PreAuthorize("@ss.hasPermi('gis:layerProductGroup:edit')")
    @Log(title = "layer分组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GisLayerProductGroup gisLayerProductGroup)
    {
        return toAjax(gisLayerProductGroupService.updateGisLayerProductGroup(gisLayerProductGroup));
    }

    @PostMapping("/sort")
    public AjaxResult updateSort(@RequestBody List<GisLayerProductGroup> groups)
    {
        return toAjax(gisLayerProductGroupService.updateGisLayerProductGroupSort(groups));
    }

    @PostMapping("/checked")
    public AjaxResult updateChecked(@RequestBody GisLayerProductGroup gisLayerProductGroup)
    {
        return toAjax(gisLayerProductGroupService.updateGisLayerProductGroupChecked(gisLayerProductGroup));
    }

    /**
     * 改变组选中状态
     * 子组 和 子图层 选中状态跟着改变
     * @param gisLayerProductGroup
     * @return
     */
    @PostMapping("/checkedGroup")
    public AjaxResult updateCheckedGroup(@RequestBody GisLayerProductGroup gisLayerProductGroup)
    {
        return toAjax(gisLayerProductGroupService.updateGisGroupChecked(gisLayerProductGroup));
    }

    /**
     * 删除layer分组
     */
//    @PreAuthorize("@ss.hasPermi('gis:layerProductGroup:remove')")
    @Log(title = "layer分组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gisLayerProductGroupService.deleteGisLayerProductGroupByIds(ids));
    }
}
